<?php

require_once 'lib.php';

/**
 * Description of ActionsConnection
 *
 * @author Markus Deutsch <Markus.Deutsch.ITM09@fh-joanneum.at>
 */
class ActionsConnection {
    private $username;
    private $password;
    public $key;
    private $connectionId;
    private $url;
    private $connected;
    private $file;
    public $status;
    
    function __construct($username = null, $password = null, $key = null) {
        include 'config.php';
        $this->key = $key;
        $this->connected = false;
        $this->connectionId = randstring();
        $this->file = $config['root'].'cookies/'.$this->connectionId;
        if(!isValidApp($key)){
            $this->status = "INVALID_KEY";
        } else if($username != null && $password != null){
            $this->setUsername($username);
            $this->setPassword($password);
        } else {
            $this->status = "INCOMPLETE_DATA";
        }
        
        $this->connect();
    }
    
    public function getConnectionStatus(){
        if(!isValidApp($this->key)){
            return "INVALID_KEY";
        } else if($this->username == null || $this->password == null){
            return "INCOMPLETE_DATA";
        } else {
            return false;
        }
    }
    
    public function setUsername($username){
        $this->username = $username;
    }
    
    public function setPassword($password){
        $this->password = utf8_encode($password);
    }
    
    public function connect(){
        if($this->username == null || $this->password == null || !isValidApp($this->key)){
            return false;
        }
        
        if($this->connected){
            return true;
        }
        
        $this->url[] = "https://actions.fh-joanneum.at/login.do";
    	$this->url[] = "https://actions.fh-joanneum.at/j_acegi_security_check";
        
        $c = curl_init($this->url[0]);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_COOKIEFILE, $this->file);
        curl_setopt($c, CURLOPT_COOKIEJAR, $this->file);
        curl_setopt($c, CURLOPT_HEADER, 1);

        $content = curl_exec($c);
        
        $cookie = explode("JSESSIONID=", $content);
        $cookie = explode(";", $cookie[1]);
        $cookie = $cookie[0];

        curl_close($c);
        
        $postdata2 = "j_domain=technikum&j_username=$this->username&submit=Anmelden&j_password=".urlencode($this->password);
    
        $d = curl_init($this->url[1]);
        curl_setopt($d, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($d, CURLOPT_HEADER, 1);
        curl_setopt($d, CURLOPT_COOKIEFILE, $this->file);
        curl_setopt($d, CURLOPT_COOKIEJAR, $this->file);
        curl_setopt($d, CURLOPT_POSTFIELDS, $postdata2);
        curl_setopt($d, CURLOPT_REFERER, $this->url[0]);
        curl_setopt($d, CURLOPT_POST, 1);
        curl_setopt($d, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($d, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        curl_setopt($d, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.04 (lucid) Firefox/3.6.23");

        $result = curl_exec($d);
        
        curl_close($d);
        
        
        if(strpos($result, "Das Benutzerkonto ist gesperrt.")){
            $this->status = "LOCKED";
        } elseif(strpos($result, "Ihre Angaben zum Anmelden sind ungültig.") || strpos($result, "login_error=1")){
            $this->status = "INVALID_DATA";
        /*} elseif (!strpos($result, "Telefonbuch")) {
            $this->status = "ERROR";*/
        } else {
            $this->status = "OK";
            $this->connected = true;
        }
        
        return true;
    }
    
    public function getFile(){
        return $this->file;
    }
    
    public function destroy(){
        // Logout on Actions Server
        $this->logout();
        
        // delete associated cookies
        $this->connectionId = null;
        $this->connected = false;
        
        // delete file
        unlink($this->file);
        
        return true;
    }
    
    private function logout(){
        $c = curl_init("https://actions.fh-joanneum.at/j_acegi_logout");
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_COOKIEFILE, $this->file);
        curl_setopt($c, CURLOPT_COOKIEJAR, $this->file);
        curl_setopt($c, CURLOPT_HEADER, 1);
        $content = curl_exec($c);
    }
    
    public function hasLoginData(){
        if($this->username != null && $this->password != null){
            return true;
        } else {
            return false;
        }
    }
    
}

?>
